package com.sea.product.service.impl;


import com.codecrane.core.Constants;
import com.codecrane.core.power.entity.PowerUser;
import com.sea.product.entity.NewProduct;
import com.sea.product.entity.NewProductRecord;
import com.sea.product.mapper.NewProductRecordMapper;
import com.sea.product.service.NewProductRecordService;
import com.sea.product.service.NewProductService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 陈鸿飞
 */
@Service
public class NewProductRecordServiceImpl implements NewProductRecordService {

    @Autowired
    private NewProductRecordMapper newProductRecordMapper;

    @Autowired
    private NewProductService newProductService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(Integer productId,Integer productStatus,String productRemark,String productTime ) {
        //更新记录
        NewProductRecord newProductRecord=new NewProductRecord();
        PowerUser user = (PowerUser) SecurityUtils.getSubject().getSession().getAttribute(Constants.CURRENT_USER_SESSION);
        newProductRecord.setProductId(productId);
        newProductRecord.setProductStatus(productStatus);
        newProductRecord.setProductTime(productTime);
        //判断是否是拒绝关闭状态
        if(productStatus>10){
            newProductRecord.setProductDate(productTime);

        }
        newProductRecord.setProductRemark(productRemark);
        newProductRecord.setProductOperate(user.getUserRealname());
        return newProductRecordMapper.insert(newProductRecord);
    }

    @Override
    public List<NewProductRecord> findByProductId(Integer productId) {
        return newProductRecordMapper.findByProductId(productId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int usave(NewProductRecord record,Integer flag) {
        NewProductRecord newProductRecord = findByProductIdStatus(record.getProductId(), record.getProductStatus());
        if (null == newProductRecord) {
            if (flag.equals(1)) {
                //更新主表状态
                NewProduct newProduct = new NewProduct();
                newProduct.setId(record.getProductId());
                if (record.getProductStatus().equals(31)) {
                    newProduct.setProductStatus(1);
                } else {
                    newProduct.setProductStatus(record.getProductStatus());
                }
                newProductService.usave(newProduct);
            }
            return save(record.getProductId(), record.getProductStatus(), record.getProductRemark(), record.getProductDate());
        } else {
            if (flag.equals(1)) {
                //更新主表状态
                NewProduct newProduct = new NewProduct();
                newProduct.setId(record.getProductId());
                if (record.getProductStatus().equals(31)) {
                    newProduct.setProductStatus(1);
                } else {
                    newProduct.setProductStatus(record.getProductStatus());
                }
                newProductService.usave(newProduct);
            }
            record.setId(newProductRecord.getId());
            return newProductRecordMapper.update(record);
        }
    }

    @Override
    public List<NewProductRecord> findTotal(Integer flag) {
        return newProductRecordMapper.findTotal(flag);
    }

    @Override
    public Integer findByMonth(Integer status, String month) {
        return newProductRecordMapper.findByMonth(status, month);
    }

    @Override
    public NewProductRecord findByProductIdStatus(Integer productId, Integer status) {
        return newProductRecordMapper.findByProductIdStatus(productId, status);
    }

    @Override
    public Integer findCountByProductDate(Integer status, String month) {
        return newProductRecordMapper.findCountByProductDate(status, month);
    }


}
